//
//  SystemUtils.m
//  SDTV_IOS
//
//  Created by sdtv on 12-12-3.
//  Copyright (c) 2012年 sdtv. All rights reserved.
//

#import "SystemUtils.h"
#import "MobClick.h"
@implementation SystemUtils


+(BOOL)needUpdated:(NSString *)newVersion
        OldVersion:(NSString *)oldVersion{
    
    NSArray*oldV = [oldVersion componentsSeparatedByString:@"."];
    NSArray*newV = [newVersion componentsSeparatedByString:@"."];
    
    if (oldV.count == newV.count) {
        for (NSInteger i = 0; i < oldV.count; i++) {
            NSInteger old = [(NSString *)[oldV objectAtIndex:i] integerValue];
            NSInteger new = [(NSString *)[newV objectAtIndex:i] integerValue];
            if (old < new) {
                return YES;
            }else if(old > new){
                return NO;
            }
        }
        return NO;
    }
    else {
        return NO;
    }
}

//判断两天是否是一天
+(BOOL)twoDateIsSameDay:(NSDate *)fist second:(NSDate *)second{
    
    
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    int unit =NSCalendarUnitMonth|NSCalendarUnitYear|NSCalendarUnitDay;
    
    NSDateComponents *fistComponets = [calendar components: unit fromDate: fist];
    NSDateComponents *secondComponets = [calendar components: unit fromDate: second];
    
    if ([fistComponets day] == [secondComponets day]
        && [fistComponets month] == [secondComponets month]
        && [fistComponets year] == [secondComponets year])
    {
        return YES;
    }
    return NO;
}

+(NSDate *)dateByString:(NSString *)str{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    
    [formatter setTimeZone:timeZone];
    
    NSArray *array = [str componentsSeparatedByString:@":"];
    if ([array count]==3) {
        [formatter setDateFormat : @"yyyy-MM-dd HH:mm:ss"];

    }else     if ([array count]==2) {
        [formatter setDateFormat : @"yyyy-MM-dd HH:mm"];
    }else {
        [formatter setDateFormat : @"yyyy-MM-dd"];
    }
    
    
    NSDate *dateTime = [formatter dateFromString:str];
    return dateTime;
}

+ (NSString*)stringByDate:(NSDate*)time
{
    NSDateFormatter* formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:@"yyyy-MM-dd HH:mm"];
    return [formater stringFromDate:time];
}


+(BOOL)date:(NSDate*)date betweenDate:(NSDate*)beginDate andDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedAscending)
        return NO;
    if ([date compare:endDate] == NSOrderedDescending)
        return NO;
    return YES;
}



+ (id)nullObjResolve:(id)text{
    if (text && ![text isEqual:[NSNull null]]
        ) {
        return text;
    }
 
    return nil;
}


+ (id)emptyStringResolve:(id)text{
    if (text && ![text isEqual:[NSNull null]] &&[text length]
        ) {
        return text;
    }

    return @"";
}


+(NSString *)handleImageUrl:(id)url{
    NSString *imageStr = [self emptyStringResolve:url];
    if([imageStr length]>0){
        if(![imageStr hasPrefix:@"http"]){
           return [NSString stringWithFormat:@"https://shop.white-food.com%@",imageStr];
        }else{
           return imageStr;
        }
        
    }

    return @"";
}

+(NSString *)revertQNBigUrl:(NSString *)url{
    return [url stringByAppendingString:@"?imageView2/2/w/480"];
}


@end
